/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cqcis.uip.base.framework.core.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

import com.cqcis.uip.base.framework.core.ResetServiceManage;
import com.cqcis.uip.base.framework.service.ResetService;
import com.cqcis.uip.base.framework.utils.TimeUtil;

/**
 * @author huangmy
 * @date   2008-5-9
 */
public class ResetServiceManageImpl implements ResetServiceManage {
	private static String SPLIT_LINE = "------------------------------------------------------------";
	private static ResetServiceManage rsm = null; // instance singleton
	private Map<String, ResetService> rsMaps = new ConcurrentHashMap<String, ResetService>();
    private AtomicLong RSID = new AtomicLong(0);

	public static synchronized ResetServiceManage getInstance() {
		if (rsm == null) {
			rsm = new ResetServiceManageImpl();
		}
		
		return rsm;
	}
	
	private ResetServiceManageImpl() {
		// do nothing
	}
	
	public Collection<ResetService> getAll() {		
		return rsMaps.values();
	}

	public String registerResetService(ResetService s) {
		long id = RSID.incrementAndGet();
		String idStr = String.valueOf(id);
		
		rsMaps.put(idStr, s);
		return idStr;
	}
	
	public ResetService getResetService(String id) {		
		return rsMaps.get(id);
	}

	public void removeResetService(ResetService s) {
		List<String> ids = new ArrayList<String>();
		for (Entry<String, ResetService> entry : rsMaps.entrySet()) {
			String id = entry.getKey();
			ResetService findS = entry.getValue();
			
			if (findS == s) {
				ids.add(id);
			}
		}
		
		for (String id : ids) {
			rsMaps.remove(id);
		}
	}
	
	public String getDescription() {		
		StringBuffer buffer = new StringBuffer();
		buffer.append(formatPrint("CACHEID", "FTIME(S)", "LASTTIME", "DESC")).append("\n")
		      .append(SPLIT_LINE).append("\n");
		
		for (Entry<String, ResetService> entry : rsMaps.entrySet()) {
			String id = entry.getKey();
			ResetService s = entry.getValue();
			
			long timeForReset = s.getTimeToRegetConf();	
			long ms = timeForReset%1000;
			String timeForResetStr = null;
			if (ms > 0) {
				timeForResetStr = timeForReset/1000 + "." + ms;
			} else {
				timeForResetStr = timeForReset/1000 + "";
			}
			 
			String lastTime = TimeUtil.getTimeStr(s.getLastResetTime());
			
			buffer.append(formatPrint(id, timeForResetStr, lastTime, s.getDescription())).append("\n");
		}
		
		buffer.append(">> Total Cache Number: ").append(rsMaps.size());
				
		return buffer.toString();
	}
	
	private static String formatPrint(String id, String intervalTime, String lastResetTime, String desc) {		
		intervalTime = intervalTime.length() > 8 ? intervalTime.substring(0, 8) : intervalTime;
		lastResetTime = lastResetTime.length() > 19 ? lastResetTime.substring(0, 19) : lastResetTime;
				
		String str = String.format(Locale.CHINESE,"%7s    %8s    %19s    %s", id, intervalTime, lastResetTime, desc);
		
		return str;
	}
}
